package com.yousu.wxqropendoor.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yousu.wxqropendoor.entity.ManageCommunity;
import com.yousu.wxqropendoor.mapper.ManageCommunityMapper;
import com.yousu.wxqropendoor.service.IManageCommunityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yousu.wxqropendoor.utils.AppResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cwb
 * @since 2020-02-28
 */
@Service
public class ManageCommunityServiceImpl extends ServiceImpl<ManageCommunityMapper, ManageCommunity> implements IManageCommunityService {
    @Autowired
    private ManageCommunityMapper manageCommunityMapper;

    /**
     * 微信小程序特殊格式适配
     *
     * @return AppResult
     */
    @Override
    public Map<String, Object> listOnWx(Integer status) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        //循环26个字母,小写
        char[] abc = new char[26];
        String type = "";
        for (int i = 0; i < abc.length; i++) {
            // 设置本次循环的首字母type
            abc[i] = (char) ('a' + i);
            type = String.valueOf(abc[i]).toLowerCase();
            // 按首字母查询
            QueryWrapper<ManageCommunity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", type);
            queryWrapper.eq("status", status);
            List<ManageCommunity> manageCommunityList = manageCommunityMapper.selectList(queryWrapper);
            Map<String, Object> objectMap = new LinkedHashMap<>(16);
            objectMap.put("id", type.toUpperCase());
            objectMap.put("items", manageCommunityList);
            if (manageCommunityList.size() == 0) {
                objectMap.put("enabled", false);
            } else {
                objectMap.put("enabled", true);
            }
            mapList.add(objectMap);
        }
        Map<String, Object> outputMap = new HashMap<>(16);
        outputMap.put("default", mapList);
        return outputMap;
    }
}
